package com.qf.user.controller;

import com.qf.common.core.annotation.NoLogin;
import com.qf.common.core.utils.R;
import com.qf.user.factory.loginfactory.LoginRationFactory;
import com.qf.user.factory.registerfactory.RegistrationFactory;
import com.qf.user.param.LoginParam;
import com.qf.user.param.RegisterParam;
import com.qf.user.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;

/**
 * @author zdl
 * @date 2025/3/21
 */
//修饰控制器类，使验证参数的注解生效
@Validated
@RestController
@RequestMapping("/user")
public class LoginController {
    @Resource
    private UserService userService;
    @Resource
    private RegistrationFactory registrationFactory;
    @Resource
    private LoginRationFactory loginRationFactory;

    /**
     * 登录
     * @param loginParam
     * @return
     */
    @NoLogin
    @PostMapping("/login")
    public R<String> loginUser(@RequestBody @Valid LoginParam loginParam){
        return R.ok(userService.login(loginParam));
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @NoLogin
    @GetMapping("/code/create")
    public R<String> sendCode(@Pattern(regexp = "^1[34578]\\d{9}$", message = "手机号码格式不正确") String phone){
        return R.ok(userService.verificationCode(phone));
    }

    /**
     * 注册
     * @param registerParam
     * @return
     */
    @NoLogin
    @PostMapping("/register")
    public R<Integer> registerUser(@RequestBody RegisterParam registerParam){
        return R.ok(userService.register(registerParam));
    }





    @PostMapping("/registerFactory")
    public R<Integer> registerUserFactory(@RequestBody RegisterParam registerParam){
        int i = registrationFactory.registration(registerParam.getType()).register(registerParam);
        return R.ok(i);
    }
    @PostMapping("/loginFactory")
    public R<String> loginUserFactory(@RequestBody LoginParam loginParam){
        String token = loginRationFactory.createLoginRation(loginParam.getLoginType()).login(loginParam);
        return R.ok(token);
    }
}
